//
//  ClassCode.swift
//  BookStore2.0
//
//  Created by hw on 2022/6/3.
//
import Foundation
class BookCopy
{
    var id:String=""
    var title:String=""
    var amount:Int=1
    var eachprice:Float=0.0
}
class ShoppingCart {
    static let sharedinstance=ShoppingCart()
    var cartContents=[BookCopy]()
    var bookCopiesData:[[String:AnyObject]]?
    var bookCopyHandler=BookCopyService()
    func addBookCopy(bookcopy:BookCopy){
       cartContents.append(bookcopy)
    }
    func deleteBookCopy(index:Int) {
    cartContents.remove(at: index)
    }
    func findAndSetAmount(booktitle:String,newAmount:Int){
        for bookcopy in cartContents {
            if bookcopy.title==booktitle {
                bookcopy.amount=newAmount
            }
        }
    }
    func getTotalCost()->Float{
        var cost:Float=0
        for bookcopy in cartContents {
            cost+=(bookcopy.eachprice*Float(bookcopy.amount))
        }
        return cost
    }
    func getTotalNum() -> Int {
        var num:Int=0
        for bookcopy in cartContents {
            num+=bookcopy.amount
        }
        return num
    }
    func ifAlreadyExists(id:String) -> Int {
        var theIndex:Int = -1
        for bookcopy in cartContents {
            theIndex=theIndex+1
            if bookcopy.id==id {
                return theIndex
            }
        }
        return -1
    }
    func importBookCopy(){
        let bookcopydatas=DB1.selectAllBookCopy()
        if bookcopydatas != nil {
            for bookcopydata in bookcopydatas! {
                let bookcopy=bookCopyHandler.BookCopyDeserialize(bookcopydata: bookcopydata)!
                addBookCopy(bookcopy: bookcopy)
                updateBookCopiesData()
            }
        }else{
            return
        }
    }
    func updateBookCopiesData(){
        if cartContents.count==0 {
            return
        }
        else{
            var bookcopiesdata=[[String:AnyObject]]()
            for bookcopy in cartContents {
                bookcopiesdata.append(bookCopyHandler.BookCopySerialize(bookcopy: bookcopy))
            }
            bookCopiesData=bookcopiesdata
        }
    }
    func outputBookCopy() {
        if cartContents.count != 0 {
            for bookcopy in cartContents {
                DB1.insertBookCopy(bookcopy: bookcopy)
            }
        }
    }
    func outputOrderItem(orderid:Int){
        for bookcopy in cartContents {
            DB1.insertOrderItem(bookcopy: bookcopy, orderid: orderid)
        }
    }
    func clearAll() {
        cartContents.removeAll()
        DB1.clearBookCopy()
    }
}
class Order {
    var id:Int=0
    var booknumSum:Int=0
    var moneySum:Float=0
    var ordertime:Date?
    init(booknumSum:Int,moneySum:Float,ordertime:Date) {
        self.booknumSum=booknumSum
        self.moneySum=moneySum
        self.ordertime=ordertime
    }
    init() {
    }
}
class OrderList {
    static let sharedinstance=OrderList()
    var orderlist:[[String:AnyObject]]?
}
class OrderServie {
    let bookcopyService=BookCopyService()
    func OrderSerialize(order:Order) -> [String:AnyObject] {
        var OrderData=[String:AnyObject]()
        OrderData["booknumSum"]=order.booknumSum as AnyObject?
        OrderData["id"]=order.id as AnyObject?
        OrderData["moneySum"]=order.moneySum as AnyObject?
        OrderData["ordertime"]=order.ordertime as AnyObject?
        return OrderData
    }
    func  OrderDeserialize(OrderData:[String:AnyObject])->Order{
        let order=Order()
        order.booknumSum=(OrderData["booknumSum"] as! NSNumber).intValue
        order.moneySum=(OrderData["moneySum"] as! NSNumber).floatValue
        order.id=(OrderData["id"] as! NSNumber).intValue
        order.ordertime=(OrderData["ordertime"]) as? Date
        return order
    }
}
class BookCopyService {
    func BookCopySerialize(bookcopy:BookCopy) ->[String:AnyObject] {
        var BookCopyData=[String:AnyObject]()
        BookCopyData["id"]=bookcopy.id as AnyObject?
        BookCopyData["title"]=bookcopy.title as AnyObject?
        BookCopyData["amount"]=bookcopy.amount as AnyObject?
        BookCopyData["eachprice"]=bookcopy.eachprice as AnyObject?
        return BookCopyData
    }
    func BookCopyDeserialize(bookcopydata:[String:AnyObject]?) -> BookCopy? {
        if bookcopydata != nil{
            let bookcopy=BookCopy()
            bookcopy.id=(bookcopydata!["id"] as? String)!
            bookcopy.eachprice=(bookcopydata!["eachprice"] as! NSNumber).floatValue
            bookcopy.amount=(bookcopydata!["amount"] as! NSNumber).intValue
            bookcopy.title=(bookcopydata!["title"] as? String)!
            return bookcopy
        }
        else{
            return nil
        }
    }
}
